package com.ddw.test;

import java.awt.Color;
import java.awt.geom.Rectangle2D;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.poi.sl.usermodel.TextShape.TextDirection;
import org.apache.poi.sl.usermodel.VerticalAlignment;
import org.apache.poi.xslf.usermodel.XMLSlideShow;
import org.apache.poi.xslf.usermodel.XSLFSlide;
import org.apache.poi.xslf.usermodel.XSLFTextBox;

/**
 * PPT文件水印添加
 */
public class PPTWaterMarkUtil {

    public static void setPPTWaterMark(String path, String targetpath, String markStr) throws IOException {

        XMLSlideShow slideShow = new XMLSlideShow(new FileInputStream(path));
        double x = slideShow.getPageSize().getWidth();
        double y = slideShow.getPageSize().getHeight();
        for (XSLFSlide slide : slideShow.getSlides()) {
            for (int j = 0; j < 3; j++) {
//	                for (int k=0; k< 2; k++) {
//	                    
                XSLFTextBox textBox = slide.createTextBox();
                textBox.setTextDirection(TextDirection.VERTICAL_270);//设置文本框文字方向
                textBox.setVerticalAlignment(VerticalAlignment.MIDDLE);
                textBox.setText(markStr);
                textBox.setAnchor(new Rectangle2D.Double(x / 3 * j + 90, y / 2, 25, 160)); // 设置水印文本框的位置和大小
                textBox.setRotation(45); // 设置水印文本框的旋转角度
//	                    textBox.setFillColor(new Color(0, 0, 0, 128)); // 设置水印文本框的填充颜色
                textBox.setLineColor(new Color(0, 0, 0, 128));
//	                    textBox.setLineWidth(1);

//	                }
            }
        }

        FileOutputStream out = new FileOutputStream(targetpath);
        slideShow.write(out);
        out.close();
    }

    public static void main(String[] args) {
        try {
            setPPTWaterMark("D:\\20240801-段德文-钱包功能简单介绍.pptx", "D:\\watermark0.pptx", "duandewen@GMM");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}